

let Service = require('./dal/service.js')
// let MC = require('./dal/matrixbox_collection.js')
let Matrixbox = require('./dal/matrixbox.js')
let User = require('./dal/user.js')
let AT = require('./dal/admin_tasks.js')
let jwt = require('../../utils/jwt')

// Token基本配置
const secretkey = "xfKey";
const ValidTime = 1800; // 30分钟的Token有效期

let Dals = {
    '/admin/service': Service,
    '/admin/matrixbox': Matrixbox,
    '/admin/user': User,
    '/admin/admin_tasks': AT,
    '/admin/xcx_syvanke_activity_0521': require('./dal/xcx_syvanke_activity_0521.js'),
}

const timeNow = () => {
    return parseInt(Date.now() / 1000);
}

const checkToken = function (token) {
    // let body = req.body || req.query;

    return new Promise((resolve, reject) => {

        // 是否存在
        if (!token) {
            return resolve({ error_code: 1, msg: "无token" });
        }

        // 是否有效
        var now = timeNow();
        jwt.decode(token)
            .then((data) => {
                jwt.encode(secretkey, { 'userid': data.userid, 'exp': timeNow() + ValidTime }).then((token) => {
                    resolve({ token: token, userid: data.userid }); // 更新token
                });
            }, (err) => {
                resolve({ error_code: 1, msg: "无效token" });
            })
    })
}


const getData = async function (req, res) {

    var body = req.body || req.query;
    var checkResult = await checkToken(body.token);
    if (checkResult.error_code) return res.send(checkResult);
    delete body.token;

    let dalItem = new Dals[req.route.path](body);

    // 返回其中一个数据
    if (body.appid) {
        let results = await dalItem.select();
        return res.send({ result: results[0], token: checkResult.token })
    } else {
        let serch = null;
        if (body.serchStr && body.serchId) {
            serch = {};
            serch[body.serchId] = body.serchStr;
        }
        let results = await dalItem.likeSelectAll(serch, body.offset, body.limit, body.orderby, body.sort);
        let comments = await dalItem.comments();
        let count = await dalItem.likeCount(serch);
        return res.send({
            result: {
                list: results,
                count: count[0].num,
                comments: comments
            },
            token: checkResult.token
        })

    }
}

const addData = async function (req, res) {
    var body = req.body || req.query;
    var checkResult = await checkToken(body.token);
    if (checkResult.error_code) return res.send(checkResult);
    delete body.token;

    let dalItem = new Dals[req.url](body);

    var results = await dalItem.add(req);

    return res.send({ token: checkResult.token })
}

const updateData = async function (req, res) {
    var body = req.body || req.query;
    var checkResult = await checkToken(body.token);
    if (checkResult.error_code) return res.send(checkResult);
    delete body.token;

    let dalItem = new Dals[req.url](body);

    var results = await dalItem.update();

    return res.send({ token: checkResult.token })
}

const delData = async function (req, res) {
    var body = req.body || req.query;
    var checkResult = await checkToken(body.token);
    if (checkResult.error_code) return res.send(checkResult);
    delete body.token;

    let dalItem = new Dals[req.url](body);

    var results = await dalItem.delete();

    return res.send({ token: checkResult.token })
}

for (let i in Dals) {
    server.get(i, getData);
    server.post(i, addData);
    server.put(i, updateData);
    server.del(i, delData);
}
